package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author msjykf
 * 客户资料处理
 * {"sk_invpty":"","invpty_name":"","cust_type":"","fundacco":"","std_idno":"","mobile":"","tradeaccount":""}; 
 *
 */

public class CustInfoDeal implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(CustInfoDeal.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			def jsonbody = Util.getRequestBody(request,'utf-8'); //获取报文
			//log.debug("jsonbody:"+jsonbody);
			if(jsonbody.equals("")) {
				return "{\"msg\": \"未获取到入参报文！\",\"code\": -2}"
			};
			JSONObject obj = JSONObject.fromObject(jsonbody);
			def sk_invpty = Util.filterForObject(obj.get("sk_invpty")); //客户编号
			def invpty_name = Util.filterForObject(obj.get("invpty_name")); //客户名称
			def cust_type = Util.filterForObject(obj.get("cust_type")); //客户类型
			def fundacco = Util.filterForObject(obj.get("fundacco")); //基金账号
			def std_idno = Util.filterForObject(obj.get("std_idno")); //证件号码
			def mobile = Util.filterForObject(obj.get("mobile")); //手机号码
			def tradeaccount = Util.filterForObject(obj.get("tradeaccount")); //交易账号
			if(sk_invpty.equals("") && invpty_name.equals("")  && fundacco.equals("") && std_idno.equals("") && mobile.equals("") && tradeaccount.equals("")) {
				return "{\"msg\": \"查询条件不能同时为空！\",\"code\": -2}"
			};
			StringBuffer params = new StringBuffer();
			if(!sk_invpty.equals("")) {
				params.append("\n"+"<params><name>SK_INVPTY</name><operator>equal</operator><value>"+sk_invpty+"</value></params>");
			};
			if(!invpty_name.equals("")) {
				params.append("\n"+"<params><name>INVPTY_NAME</name><operator>like</operator><value>"+invpty_name+"</value></params>");
			};
			if(!cust_type.equals("")) {
				params.append("\n"+"<params><name>DK_CUST_TYPE</name><operator>equal</operator><value>"+cust_type+"</value></params>");
			};
			if(!fundacco.equals("")) {
				params.append("\n"+"<params><name>FUNDACCO</name><operator>equal</operator><value>"+fundacco+"</value></params>");
			}else{
				params.append("\n"+"<params><name>FUNDACCO</name><operator>equal</operator><value>-1</value></params>");
			};
			if(!std_idno.equals("")) {
				params.append("\n"+"<params><name>STD_IDNO</name><operator>equal</operator><value>"+std_idno+"</value></params>");
			};
			if(!mobile.equals("")) {
				params.append("\n"+"<params><name>MOBILE</name><operator>equal</operator><value>"+mobile+"</value></params>");
			};
			if(!tradeaccount.equals("")) {
				params.append("\n"+"<params><name>BK_TRADEACCOUNT</name><operator>equal</operator><value>"+tradeaccount+"</value></params>");
			}else{
				params.append("\n"+"<params><name>BK_TRADEACCOUNT</name><operator>equal</operator><value>-1</value></params>");
			};
			//log.debug("params:"+params);
			PropertiesUtil properties = new PropertiesUtil();
			def ibqurl = properties.readValue("ibqurl"); //获取webservice接口地址
			def appid = properties.readValue("appid"); //获取appId
			def charset = "utf-8"; //字符集
			def timeout = 100000; //请求超时时间
			def xml = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
							<SOAP-ENV:Body>
								<m:query xmlns:m="http://service.web.server.ua.businessmatrix.com/">
									<appId>"""+appid+"""</appId>
									<name>s-CUSTINTO</name>"""+params+"""
								</m:query>
							</SOAP-ENV:Body>
						</SOAP-ENV:Envelope>""";
			//log.debug("xml:"+xml);
			def retrunxml = Util.callWebService(ibqurl,xml,charset,"",timeout);
			retrunxml = Util.Transform(retrunxml);
			//log.debug("retrunxml:"+retrunxml);
			def list = new XmlParser().parseText(retrunxml);
			int c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24;
			def i = 0;
			for(it in list.Body.queryResponse.QueryResult.columns) {
				//log.debug("it:"+it);
				switch(it.name.text()) {
					case 'SK_INVPTY':
					c1 = i;
					break;
					case 'INVPTY_NAME':
					c2 =i;
					break;
					case 'DK_ID_TYPE':
					c3 =i;
					break;
					case 'STD_IDNO':
					c4 =i;
					break;
					case 'DK_CUST_TYPE':
					c5 =i;
					break;
					case 'DK_SEX':
					c6 =i;
					break;
					case 'MOBILE':
					c7 =i;
					break;
					case 'DK_RISK_TYPE':
					c8 =i;
					break;
					case 'BK_FUNDACCOUNT':
					c9 =i;
					break;
					case 'STATUS':
					c10 =i;
					break;
					case 'DK_CUST_LEVEL':
					c11 =i;
					break;
					case 'BIRTHDAY':
					c12 =i;
					break;
					case 'DK_INDUSTRY':
					c13 =i;
					break;
					case 'PHONE':
					c14 =i;
					break;
					case 'EMAIL':
					c15 =i;
					break;
					case 'ZIPCODE':
					c16 =i;
					break;
					case 'ADDRESS':
					c17 =i;
					break;
					case 'DK_INVEST_TYPE':
					c18 =i;
					break;
					case 'CONT_NAME':
					c19 =i;
					break;
					case 'DK_CON_TYPE':
					c20 =i;
					break;
					case 'CONT_IDCARDNO':
					c21 =i;
					break;
					case 'LAW_NAME':
					c22 =i;
					break;
					case 'BK_TRADEACCOUNT':
					c23 =i;
					break;
					case 'FUNDACCO':
					c24 =i;
					break;
				};
				i++;
			};
			def invptyid,invptyname,idtype,idno,custtype,sex,custmobile,risktype,fundaccount,status,custlevel,birthday,industry,phone,email,zipcode,address,investtype,contname,contype,cont_idcardno,law_name,custtradeaccount,custfundacco;
			for(it in list.Body.queryResponse.QueryResult.rows) {
				invptyid = it.item[c1].text();
				invptyname = it.item[c2].text();
				idtype = it.item[c3].text();
				idno = it.item[c4].text();
				custtype = it.item[c5].text();
				sex = it.item[c6].text();
				custmobile = it.item[c7].text();
				risktype = it.item[c8].text();
				fundaccount = it.item[c9].text();
				status = it.item[c10].text();
				custlevel = it.item[c11].text();
				birthday = it.item[c12].text();
				industry = it.item[c13].text();
				phone = it.item[c14].text();
				email = it.item[c15].text();
				zipcode = it.item[c16].text();
				address = it.item[c17].text();
				investtype = it.item[c18].text();
				contname = it.item[c19].text();
				contype = it.item[c20].text();
				cont_idcardno = it.item[c21].text();
				law_name = it.item[c22].text();
				custtradeaccount = it.item[c23].text();
				custfundacco = it.item[c24].text();
				break;
			};
			//判断时间是否为null
			if(birthday==null){
				birthday="";
			}
			log.debug("invptyid:"+invptyid);
			def customer_guid;
			if(!invptyid.equals("")&&invptyid!=null) {
				def smap = eliteBean.chaxun_sql("select customer_guid from customer where customer_id="+invptyid+" and rownum=1","dataSource");
				//log.debug("smap:"+smap);
				//log.debug("smapnum:"+smap.size());
				
				if(smap.size()==0) {
					customer_guid = Util.getGuid();
					eliteBean.update("insert into customer(customer_guid,customer_id,customertype_id,customername,idtype,customerid,gender,mobile,tel_1,tel_2,e_mail,zipcode,stringfield2,stringfield3,stringfield4,customerlevel,birthday,stringfield9,address,stringfield8,createdby,createddate,stringfield1,stringfield101,stringfield102,stringfield103,stringfield104)values('"+customer_guid+"',"+invptyid+",'"+custtype+"','"+invptyname+"','"+idtype+"','"+idno+"','"+sex+"','"+custmobile+"','"+phone+"','','"+email+"','"+zipcode+"','"+risktype+"','"+fundaccount+"','"+status+"','"+custlevel+"','"+birthday+"','"+industry+"','"+address+"','"+investtype+"','SELITE',sysdate,'anzheng','"+contname+"','"+contype+"','"+cont_idcardno+"','"+law_name+"')","dataSource");
				}else {
					customer_guid = smap[0].get("customer_guid");

					eliteBean.update("update customer set customertype_id='"+custtype+"',customername=case when '"+invptyname+"' ='' or '"+invptyname+"' is null then customername else '"+invptyname+"' end ,idtype='"+idtype+"',customerid='"+idno+"',gender='"+sex+"',stringfield2='"+risktype+"',stringfield3='"+fundaccount+"',stringfield4='"+status+"',customerlevel='"+custlevel+"',birthday='"+birthday+"',stringfield9='"+industry+"',stringfield8='"+investtype+"',stringfield101='"+contname+"',stringfield102='"+contype+"',stringfield103='"+cont_idcardno+"',stringfield104='"+law_name+"',mobile=case when '"+custmobile+"' ='' or '"+custmobile+"' is null then mobile else '"+custmobile+"' end,tel_1=case when '"+phone+"' ='' or '"+phone+"' is null then tel_1 else '"+phone+"' end  where customer_guid='"+customer_guid+"'","dataSource");
				};
				return "{\"msg\": \"\",\"code\": 1,\"customer_guid\": \""+customer_guid+"\",\"sk_invpty\": "+invptyid+",\"invpty_name\": \""+invptyname+"\"}";
			}else{
				return "{\"msg\": \"查询记录为空！\",\"code\": -2}";
			};
			
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			return "{\"msg\": \"请求接口失败！\",\"code\": -1}";
		}
	}
}